package com.example.lotteryticketdev.tool;

import com.alibaba.fastjson.JSONObject;
import com.example.lotteryticketdev.entity.SpiderResult;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.net.URL;

public class SpiderTool {

    public static void main(String[] args) throws IOException {
        getJsonResult(1,30);
    }

    public void getPageResult()  throws IOException {

        String path =  "http://www.cwl.gov.cn/ygkj/wqkjgg/ssq/";

        URL url = new URL(path);

        Document document = Jsoup.parse(url, 3000);

        System.out.println("document = " + document);
    }

    public static SpiderResult getJsonResult(Integer pageNo,Integer pageSize){

        String url = "http://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&issueCount=&issueStart=&issueEnd=&dayStart=&dayEnd=&pageNo="+pageNo+"&pageSize="+pageSize+"&week=&systemType=PC";
//        String url = "http://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&pageNo="+pageNo+"&pageSize="+pageSize+"&systemType=PC";

        String text = null;
        try {
            text = Jsoup.connect(url)
                    .timeout(30000)
                    .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
                    .header("Host","www.cwl.gov.cn")
                    .header("Accept-Encoding", "gzip, deflate, sdch")
                    .header("Accept-Language", "zh-CN,zh;q=0.8")
                    .header("Referer","http://www.cwl.gov.cn/ygkj/kjgg/")
                    .header("X-Requested-With","XMLHttpRequest")
                    .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
                    .get()
                    .body()
                    .text();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("text = " + text);
        SpiderResult spiderResult = JSONObject.parseObject(text, SpiderResult.class);
        return spiderResult;
    }


}
